package com.aplana.ccsd.service;

import info.magnolia.cms.core.MgnlNodeType;
import info.magnolia.cms.util.ContentUtil;
import info.magnolia.jcr.util.NodeUtil;
import info.magnolia.jcr.util.PropertyUtil;
import info.magnolia.module.blossom.dialog.TabBuilder;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.jcr.Node;
import javax.jcr.RepositoryException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aplana.ccsd.enums.TabNumber;
import com.aplana.ccsd.model.Vacancy;
import com.aplana.ccsd.util.JcrNodeUtils;

@SuppressWarnings("deprecation")
@Service
public class VacancyServiceImpl implements VacancyService {

	@Autowired
	private TemplatingService templatingService;

	@Override
	public Vacancy getVacancy(Node vacancyPage) throws RepositoryException {
		String title = PropertyUtil.getString(vacancyPage, "title");
		String description = templatingService.getNodePropertyAsHtml(
				vacancyPage, "description");
		Vacancy vacancy = new Vacancy(title, description,
				JcrNodeUtils.getURL(vacancyPage));

		vacancy.setSalaryLevel(PropertyUtil.getString(vacancyPage,
				"salaryLevel"));
		vacancy.setCity(PropertyUtil.getString(vacancyPage, "city"));
		vacancy.setRequiredExperience(PropertyUtil.getString(vacancyPage,
				"requiredExperience"));
		vacancy.setEmploymentType(PropertyUtil.getString(vacancyPage,
				"employmentType"));

		Calendar calendar = PropertyUtil
				.getDate(vacancyPage, "publicationDate");
		vacancy.setPublicationDate(calendar == null ? null : calendar.getTime());

		vacancy.setResponsibility(templatingService.getNodePropertyAsHtml(
				vacancyPage, "responsibility"));
		vacancy.setRequirements(templatingService.getNodePropertyAsHtml(
				vacancyPage, "requirements"));
		vacancy.setTerms(templatingService.getNodePropertyAsHtml(vacancyPage,
				"terms"));

		return vacancy;
	}

	@Override
	public void addVacancyDialogControls(TabBuilder tabBuilder,
			TabNumber tabNumber) {
		if (TabNumber.FIFTH.equals(tabNumber)) {
			tabBuilder.addEdit("title", "Название вакансии", "").setRequired(
					true);
			tabBuilder.addFckEditor("description", "Описание", "");

			tabBuilder.addEdit("salaryLevel", "Уровень зарплаты", "");
			tabBuilder.addEdit("city", "Город", "");
			tabBuilder.addEdit("requiredExperience", "Требуемый опыт работы",
					"");
			tabBuilder.addDate("publicationDate", "Дата публикации вакансии",
					"");
			tabBuilder.addEdit("employmentType", "Тип занятости", "");
		} else {
			tabBuilder.addFckEditor("responsibility", "Обязанности", "");
			tabBuilder.addFckEditor("requirements", "Требования", "");
			tabBuilder.addFckEditor("terms", "Условия", "");
		}

	}

	@Override
	public void addVacancyListDialogControls(TabBuilder tabBuilder) {
		tabBuilder.addEdit("title", "Заголовок", "");
		tabBuilder.addLink("vacanciesLink", "Страница с вакансиями",
				"Ссылка на страницу с вакансиями");

	}

	@Override
	public Node getVacancyListPage(Node component) throws RepositoryException {
		String nodeLink = PropertyUtil.getString(component, "vacanciesLink");
		return component.getSession().getNode(nodeLink);
	}

	@Override
	public List<Vacancy> findVacancies(Node vacanciesParentPage)
			throws RepositoryException {
		Iterable<Node> pages = NodeUtil.getNodes(vacanciesParentPage,
				MgnlNodeType.NT_PAGE);
		List<Vacancy> results = new ArrayList<Vacancy>();
		for (Node page : pages) {
			if (!ContentUtil.asContent(page).hasMixin(MgnlNodeType.MIX_DELETED)) {
				Vacancy item = getVacancy(page);
				results.add(item);
			}
		}
		return results;
	}

}